﻿///////////////////////////////////////////////////////////////////////////////////////////////////
// AmiBroker Plug-in SDK | Copyright © 2010 by Koistya `Navin | http://code.google.com/p/amibroker/
// ------------------------------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////////////////////////////

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AmiBrokerPlugin.Tests
{
    [TestClass]
    public class PluginTest
    {
        [TestMethod]
        public void GetPluginInfo_Should_Return_Plugin_Info()
        {
            // Arrange
            var pluginInfo = new PluginInfo();

            // Act
            var result = Plugin.GetPluginInfo(ref pluginInfo);

            // Assert
            Assert.AreEqual(1, result);
            Assert.AreEqual("AmiBroker® data Plug-in", pluginInfo.Name);
            Assert.AreEqual("AmiBroker.CodePlex.com", pluginInfo.Vendor);
            Assert.AreEqual(1095584080, pluginInfo.IDCode);
            Assert.AreEqual(PluginType.Data, pluginInfo.Type);
            Assert.AreEqual(10000, pluginInfo.Version);
            Assert.AreEqual(0, pluginInfo.Certificate);
            Assert.AreEqual(387000, pluginInfo.MinAmiVersion);
        }

        [TestMethod]
        public void Init_Test()
        {
            // Act
            var result = Plugin.Init();

            // Assert
            Assert.AreEqual(1, result);
        }

        [TestMethod]
        public void Release_Test()
        {
            // Act
            var result = Plugin.Release();

            // Assert
            Assert.AreEqual(1, result);
        }

        [TestMethod]
        unsafe public void GetQuotesEx_Test()
        {
            // Arrange
            var quotes = stackalloc Quotation[250];
            var context = stackalloc GQEContext[1];

            // Act & Assert
            var result = Plugin.GetQuotesEx("URSI", Periodicity.EndOfDay, -1, 250, quotes, context);
            Assert.IsTrue(result > 0);

            result = Plugin.GetQuotesEx("URSI", Periodicity.EndOfDay, result - 1, 250, quotes, context);
            Assert.IsTrue(result > 0);

            result = Plugin.GetQuotesEx("URSI", Periodicity.EndOfDay, result - 2, 250, quotes, context);
            Assert.IsTrue(result > 0);
        }

        [TestMethod]
        public void Notify_Test()
        {
            var si = new StockInfo { ShortName = "GAZP" };
            var pn = new PluginNotification { StockInfo = GCHandle.ToIntPtr(GCHandle.Alloc(si)) };
            Marshal.StructureToPtr(si, pn.StockInfo, true);
            Plugin.Notify(ref pn);
        }

        [TestMethod]
        public void Configure_Test()
        {
            var ptr = Marshal.AllocHGlobal(32);
            Plugin.Configure("", ptr);
        }
    }
}
